//token验证
function seller_login_verification(token) {
    var url = base_url + "/user/seller/info";
    var return_info;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "token": token
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                return_info = data.data;
            } else if (Math.floor(data.code / 100) === 2) {
                alert_and_redirect(data.msg + ",3秒后跳转到登录页...", "../user/login.html", 3);
            } else {
                alert_to_user(data.msg);
            }
        },
        error: function () {
            alert_to_user("请求网页失败");
        }
    })
    return return_info;
}

//获取商品列表
function product_seller_list(token, page) {
    var url = base_url + "/product/seller/list";
    var return_info;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "token": token,
            "page": page
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                return_info = data.data;
            } else if (Math.floor(data.code / 100) === 2) {
                alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
            } else {
                alert_to_user(data.msg);
            }
        },
        error: function () {
            alert_to_user("请求网页失败");
        }
    })
    return return_info;
}

//上传商品信息
function product_seller_upload(formData) {
    var url = base_url + "/product/seller/upload";
    var status = false;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: formData,
        processData: false,//*
        contentType: false,//*
        success: function (data) {
            if (data.code === 0) {
                status = true;
                alert_to_user("上传商品成功");
            } else if (Math.floor(data.code / 100) === 2) {
                alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
            } else {
                alert_to_user(data.msg);
            }
        },
        error: function () {
            alert_to_user("请求失败");
        }
    })
    return status;
}

//更新商品信息
function product_seller_update(formData) {
    var url = base_url + "/product/seller/update";
    var status = false;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: formData,
        processData: false,//*
        contentType: false,//*
        success: function (data) {
            if (data.code === 0) {
                status = true;
                alert_to_user("修改商品成功");
            } else if (Math.floor(data.code / 100) === 2) {
                alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
            } else {
                alert_to_user(data.msg);
            }
        },
        error: function () {
            alert_to_user("请求失败");
        }
    })
    return status;
}

//获取商品信息
function product_seller_get(token, productId) {
    var url = base_url + "/product/seller/get";
    var return_info;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "token": token,
            "productId": productId
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                return_info = data.data;
            } else if (Math.floor(data.code / 100) === 2) {
                alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
            } else {
                alert_to_user(data.msg);
            }
        },
        error: function () {
            alert_to_user("请求网页失败");
        }
    })
    return return_info;
}

//获取订单列表
function order_seller_list(token, page) {
    var url = base_url + "/order/seller/list";
    var return_info;
    $.ajax({
        type: "post",    //请求方式
        async: false,    //是否异步
        url: url,
        data: {
            "token": token,
            "page": page
        },    //请求参数  token
        success: function (data) {
            if (data.code === 0) {
                return_info = data.data;
            } else if (Math.floor(data.code / 100) === 2) {
                alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
            } else {
                alert_to_user(data.msg);
            }
        },
        error: function () {
            alert_to_user("请求网页失败");
        }
    })
    return return_info;
}

//停售商品
function product_seller_delete(productId) {
    confirm_to_user("确认是否停售商品。<br>注意：如果停售商品VIP状态会一并取消！", function () {
        var url = base_url + "/product/seller/delete";
        $.ajax({
            type: "post",    //请求方式
            async: false,    //是否异步
            url: url,
            data: {
                "token": localStorage.getItem("token"),
                "productId": productId
            },    //请求参数  token
            success: function (data) {
                if (Math.floor(data.code / 100) === 2) {
                    alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
                } else {
                    alert_to_user(data.msg);
                    product_list_page();
                }
            },
            error: function () {
                alert_to_user("请求网页失败");
            }
        })
    })
}

//重售商品
function product_seller_reshelve(productId) {
    confirm_to_user("确认是否重新上架商品", function () {
        var url = base_url + "/product/seller/reshelve";
        $.ajax({
            type: "post",    //请求方式
            async: false,    //是否异步
            url: url,
            data: {
                "token": localStorage.getItem("token"),
                "productId": productId
            },    //请求参数  token
            success: function (data) {
                if (Math.floor(data.code / 100) === 2) {
                    alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
                } else {
                    alert_to_user(data.msg);
                    product_list_page();
                }
            },
            error: function () {
                alert_to_user("请求网页失败");
            }
        })
    })
}

//升级vip
function product_seller_vip(productId, level) {
    var content;
    var money;
    if (level === 2) {
        content = "确认是否开通<b>普通VIP</b>";
        money = 200;
    } else if (level === 3) {
        content = "确认是否开通<b>超级VVIP</b>";
        money = 500;
    } else if (level === 4) {
        content = "确认是否开通<b>至尊VVVIP</b>";
        money = 1000;
    } else {
        alert_to_user("参数有误！");
    }

    confirm_to_user(content, function () {
        pay_confirm(money, function () {
            var url = base_url + "/product/seller/vip";
            $.ajax({
                type: "post",    //请求方式
                async: false,    //是否异步
                url: url,
                data: {
                    "token": localStorage.getItem("token"),
                    "productId": productId,
                    "level": level
                },    //请求参数  token
                success: function (data) {
                    if (Math.floor(data.code / 100) === 2) {
                        alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
                    } else {
                        alert_to_user(data.msg);
                        product_list_page();
                    }
                },
                error: function () {
                    alert_to_user("请求网页失败");
                }
            })
        });
    })

}



//发货
function order_seller_deliver(orderId) {
    var courierNumber;
    $.confirm({
        title: '发货信息',
        content:
            '<form action="" class="formName">' +
            '<div class="form-group">' +
            '<div>订单id：' + orderId + '</div><br>' +
            '<label>请输入发货物流编号(没有物流则不用输入)</label>' +
            '<input type="text" placeholder="物流编号" class="name form-control" required />' +
            '</div>' +
            '</form>',
        buttons: {
            formSubmit: {
                text: '提交',
                btnClass: 'btn-blue',
                action: function () {
                    courierNumber = this.$content.find('.name').val();
                    if (!courierNumber) {
                        courierNumber = "无物流信息";
                    }
                    //发货逻辑
                    var url = base_url + "/order/seller/deliver";
                    $.ajax({
                        type: "post",    //请求方式
                        async: false,    //是否异步
                        url: url,
                        data: {
                            "token": localStorage.getItem("token"),
                            "orderId": orderId,
                            "courierNumber": courierNumber
                        },    //请求参数  token
                        success: function (data) {
                            if (Math.floor(data.code / 100) === 2) {
                                alert_and_redirect(data.msg + ",3秒后跳转到登录页", "../user/login.html", 3);
                            } else {
                                alert_to_user(data.msg);
                                order_list_page();
                            }
                        },
                        error: function () {
                            alert_to_user("请求网页失败");
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                action: function () {
                    alert_to_user("您取消了发货")
                }
            },
        }
    });

}